{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from os.path import join\n",
    "from SWOTRiver import SWOTL2\n",
    "from RiverObs import ReachExtractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from os.path import exists\n",
    "def find_riverobs_test_data_dir():\n",
    "    \"\"\"Fin the location of the test data root directory\"\"\"\n",
    "    \n",
    "    if 'RIVEROBS_TESTDATA_DIR' in os.environ:\n",
    "        test_data_dir = os.environ('RIVEROBS_TESTDATA_DIR')\n",
    "    else: # try the default location\n",
    "        test_data_dir = '../../../RiverObsTestData'\n",
    "        \n",
    "    if not exists(test_data_dir):\n",
    "        print('You must either set the environment variable RIVEROBS_TESTDATA_DIR')\n",
    "        print('or locate the test data directory at ../../../RiverObsTestData')\n",
    "        raise Exception('Test data directory not found.')\n",
    "        \n",
    "    return test_data_dir\n",
    "\n",
    "data_dir = find_riverobs_test_data_dir()\n",
    "data_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l2_file = join(data_dir,'L2','L2v1','swot_heights_ohio_example_v1.Multilook_L2PIXC.nc')\n",
    "assert exists(l2_file)\n",
    "\n",
    "db_dir = join(data_dir,'GRWDL','nAmerica_GRWDL_river_topo')\n",
    "shape_file_root = join(db_dir,'nAmerica_GRWDL_river_topo')\n",
    "shape_file_root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lonmin =  -83 \n",
    "latmin =  38\n",
    "lonmax =  -82\n",
    "latmax =  39\n",
    "bounding_box = lonmin,latmin,lonmax,latmax\n",
    "\n",
    "# The list of classes to consider for potential inundation.\n",
    "# The truth classes are [1], if no_layover_classification' is used.\n",
    "# If estimated classification is used, the choice depends on whether\n",
    "# use_fractional_inundation is set.\n",
    "# If it is not set, either [3,4] or [4] should be used.\n",
    "# If it is set, [2,3,4] or [3,4] should be used.\n",
    "class_list = [2,3,4,5]\n",
    "\n",
    "lat_kwd = 'latitude_medium'\n",
    "lon_kwd = 'longitude_medium'\n",
    "class_kwd = 'classification'\n",
    "height_kwd = 'height_medium'\n",
    "\n",
    "l2 = SWOTL2(l2_file,bounding_box=bounding_box,\n",
    "            class_list=class_list,\n",
    "            lat_kwd=lat_kwd,lon_kwd=lon_kwd,class_kwd=class_kwd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reaches = ReachExtractor(shape_file_root, l2,clip_buffer=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reaches[0].metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figsize(6,6)\n",
    "scatter(l2.lon,l2.lat,edgecolor='none',alpha=0.1,label='l2 data')\n",
    "for i,reach in enumerate(reaches):\n",
    "    lon, lat = reach.lon, reach.lat\n",
    "    #plot(lon,lat,'.m',alpha=0.1)\n",
    "    plot(lon,lat,alpha=1,label='reach %d'%i)\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figsize(6,6)\n",
    "scatter(l2.x/1.e3,l2.y/1.e3,edgecolor='none',alpha=0.1,label='l2 data')\n",
    "for i,reach in enumerate(reaches):\n",
    "    x, y = reach.x, reach.y\n",
    "    #plot(x/1.e3,y/1.e3,'.m',alpha=0.1)\n",
    "    plot(x/1.e3,y/1.e3,alpha=1,label='reach %d'%i)\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reaches[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(reaches)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reaches.reach_idx"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
